package com.github.translator.example;

import com.aspose.pdf.*;
import com.github.translator.util.FileUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

public class TranslateExample {

	public static void main(String[] args) throws Exception {
		com.aspose.pdf.License license = new com.aspose.pdf.License();
		license.setLicense("Aspose.Pdf.lic");
		//translate("D:/code_repos/gitee/Aspose.PDF-for-Java-master/Examples/target/classes/DocumentConversion/sample.pdf");

		html2pdf(Paths.get("D:/code_repos/gitee/Aspose.PDF-for-Java-master/Examples/target/classes/DocumentConversion/translated/sample.html"));
	}


	public static void translate(String pdfUrl) throws Exception{
		Path pdfPath = Paths.get(pdfUrl);
		Path htmlPath = pdf2html(pdfPath);

		System.setProperty("webdriver.chrome.driver", "C:\\Users\\Think\\Downloads\\chromedriver_win32\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("file:///" + htmlPath.toString());

		// driver.manage().window().fullscreen();
		Actions actions = new Actions(driver);

		WebElement webElement = driver.findElement(By.tagName("body"));
		webElement.click();
		actions.moveToElement(webElement).contextClick().build().perform();
		// Thread.sleep(1000);
		Robot robot = new Robot();
		robot.keyPress(KeyEvent.VK_T);
		// Thread.sleep(1000);
		//
		Object height = ((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight");
		int scrollHeight = Integer.parseInt(height.toString());
		int pages = scrollHeight / 5000 + 1;
		for (int i = 0; i < pages; i++) {
			((JavascriptExecutor) driver).executeScript("window.scrollTo(0," + (i * 5000) + ")");
			Thread.sleep(1000);
		}

		actions.moveToElement(webElement).contextClick().build().perform();
		robot.keyPress(KeyEvent.VK_A);

		htmlPath = saveTranslatedHtml(htmlPath);

		html2pdf(htmlPath);

		driver.quit();
	}


	public static Path pdf2html(Path originPdfPath) throws Exception {
		Document doc = new Document(originPdfPath.toString());
		HtmlSaveOptions newOptions = new HtmlSaveOptions();
		newOptions.PartsEmbeddingMode = HtmlSaveOptions.PartsEmbeddingModes.EmbedAllIntoHtml;
		newOptions.LettersPositioningMethod = LettersPositioningMethods.UseEmUnitsAndCompensationOfRoundingErrorsInCss;
		newOptions.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground;
		newOptions.FontSavingMode = HtmlSaveOptions.FontSavingModes.SaveInAllFormats;
		String fileName = FileUtil.getNameWithoutExtension(originPdfPath);
		String htmlUrl = originPdfPath.getParent() + File.separator + fileName + ".html";
		doc.save(htmlUrl, newOptions);
		return Paths.get(htmlUrl);
	}


	private static final String auto_it_exe = "D:\\ProgramFiles\\AutoIt3\\save_as_x86.exe ";
	public static Path saveTranslatedHtml(Path originHtmlPath) throws Exception {
		StringBuilder sb = new StringBuilder(originHtmlPath.getParent().toString());
		sb.append(File.separator).append("translated").append(File.separator);
		String fileName = FileUtil.getNameWithoutExtension(originHtmlPath);
		sb.append(fileName).append(".html");
		Process process = Runtime.getRuntime().exec(auto_it_exe + sb.toString());
		process.waitFor();
		System.out.println("===========>"+process.exitValue());
		return Paths.get(sb.toString());
	}




	public static void html2pdf(Path translatedHtmlPath) throws Exception {
		File file = translatedHtmlPath.toFile();
		while (!file.exists()){
			Thread.sleep(500);
			System.out.println(translatedHtmlPath.toString());
		}
		String fileName = FileUtil.getNameWithoutExtension(translatedHtmlPath);
		HtmlLoadOptions htmloptions = new HtmlLoadOptions(translatedHtmlPath.getParent().toString());
		htmloptions.setInputEncoding("utf-8");
		htmloptions.setHtmlMediaType(HtmlMediaType.Print);
		Document doc = new Document(translatedHtmlPath.toString(), htmloptions);
		doc.save(translatedHtmlPath.getParent() + File.separator + fileName + ".pdf");
	}

}
